package com.long1008.controller.personnel;

import com.long1008.pojo.EmpMove;
import com.long1008.pojo.Employee;
import com.long1008.pojo.RespBean;
import com.long1008.pojo.RespPageEntity;
import com.long1008.service.EmpService;
import com.long1008.service.PersonnelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/personnel/remove")
public class EmpRemoveController {

    @Autowired
    PersonnelService personnelService;
    @Resource
    EmpService empService;

    @RequestMapping(value = "/move",method = RequestMethod.POST)
    public RespBean addEmpMove(EmpMove empMove){
        Employee employee = empService.getEmpById(empMove.getEid());
        if(empService.getEmpById(empMove.getEid())==null){
            return RespBean.error("员工号不存在");
        }
        if (!employee.getName().equals(empMove.getEmpName())) {
            return RespBean.error("员工号与员工姓名不匹配！");
        }
        if(personnelService.addEmpMove(empMove)==1 && personnelService.updateDepIdAndJobId(empMove)==1){
            return RespBean.ok("添加成功");
        }
        return RespBean.error("添加失败");
    }


    @RequestMapping(value = "/move",method = RequestMethod.PUT)
    public RespBean updateEmpMove(EmpMove empMove){
        Employee employee = empService.getEmpById(empMove.getEid());
        if(empService.getEmpById(empMove.getEid())==null){
            return RespBean.error("员工号不存在");
        }
        if (!employee.getName().equals(empMove.getEmpName())) {
            return RespBean.error("员工号与员工姓名不匹配！");
        }
        if(personnelService.updateEmpMove(empMove)==1 && personnelService.updateDepIdAndJobId(empMove)==1){
            return RespBean.ok("修改成功");
        }
        return RespBean.error("修改失败");
    }
    @RequestMapping(value = "/move",method = RequestMethod.GET)
    public List<EmpMove> getAllEmpMove(){
        return personnelService.getAllEmpMove();
    }
    /**
     * 根据页码和大小分页查询
     * @param page 当前页，默认为1
     * @param size 当前每页显示行数，默认为10
     * @return 页信息的实体
     */
    @GetMapping("/move/mv")
    public RespPageEntity getMovesByPage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        return personnelService.getMovesByPage(page, size);
    }
    //根据员工姓名筛选调动记录
    @RequestMapping(value = "/move/{keywords}")
    public RespPageEntity getMovesByKeywords(
            @PathVariable(required = false) String keywords,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size){
        return personnelService.getMovesByKeywords(keywords, page, size);
    }
    @RequestMapping(value = "/move/{ids}",method = RequestMethod.DELETE)
    public RespBean deleteEmpMove(@PathVariable String ids){
        if(personnelService.deleteEmpMove(ids)==1){
            return RespBean.ok("删除成功");
        }
        return RespBean.error("删除失败");
    }
}
